package com.financing.wap.web.controller.api;


import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.AdQueryVo;
import com.financing.wap.web.controller.BaseController;

/**
 * 
 * @ClassName: ADController
 * @Description: 亿起发广告接入 
 * @author  qiye
 * @date 2014年11月22日 下午3:22:39
 *
 */
@Controller("adController")
@RequestMapping("/api")
public class ADController extends BaseController {
	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IUserService userService;
	
	@RequestMapping("/ad")
	public void ad(HttpServletRequest request, HttpServletResponse response){
		
		String mid = request.getParameter("src");
		String wi = request.getParameter("wi");
		if(!StringUtil.isNullOrEmptyStr(mid)){
			SessionUtil.getSession().setAttribute("mid", mid);
		}
		if(!StringUtil.isNullOrEmptyStr(wi)){
			SessionUtil.getSession().setAttribute("wi", wi);
		}
		response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
		response.setHeader("Location", "/");
		Cookie cookie = new Cookie("ADsrc",mid);
		cookie.setMaxAge(30*24*60*60); 
		response.addCookie(cookie);
		Cookie cookie2 = new Cookie("ADwi",wi);
		cookie2.setMaxAge(30*24*60*60); 
		response.addCookie(cookie2);
	}
	@RequestMapping("/wx")
	@ResponseBody
	public ResultMap wx(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		String mid = request.getParameter("src");
		if(!StringUtil.isNullOrEmptyStr(mid)){
			SessionUtil.getSession().setAttribute("mid", mid);
		}
		
		logger.info("wx media:"+mid);
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setStatus(ResultMap.STATUS.SUCCESS);
		return result;
	}
	@RequestMapping("/adQuery")
	public void adQuery(HttpServletRequest request, HttpServletResponse response){
		String dateStr = request.getParameter("d");
		Date date = DateUtil.format(dateStr, "yyyyMMdd");
		
		List<AdQueryVo> list = userService.queryAdRegInfo(date);
		
		try {
			response.setContentType("application/x-download");
			response.addHeader("Content-Disposition", "attachment;filename=" + dateStr+".txt");
			OutputStream os = response.getOutputStream();
			Writer w = new OutputStreamWriter(os);
			for(AdQueryVo adQuery:list){
				StringBuffer stringBuffer = new StringBuffer();
				stringBuffer.append(adQuery.getWi());
				stringBuffer.append("||");
				stringBuffer.append(adQuery.getCreateDate());
				stringBuffer.append("||");
				stringBuffer.append(adQuery.getId());
				stringBuffer.append("||");
				stringBuffer.append(adQuery.getAdType());
				stringBuffer.append("||basic\r\n");
				
				w.write(stringBuffer.toString());
			}
			w.flush();
			
		} catch (IOException e) {
			log.error("get response output stream error!",e);
		}
		
	}

}
